// pages/door_service_orders/door_service_orders.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getOrderList, cancelOrder, finishOrder, refundOrder, askServiceHelp } from '../../static/api/api.js'
let payWay;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_type:10,
        status:'',
        pages: {
            curPage: 1,
            totalPage: 1
        },
        curItem:null,
        curIndex:'',
        listData: null,
        isNoMore: false,
        pageLoad: false,
        isShowDetail:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            'orderStatus': utils.orderStatus
        })
        payWay = this.selectComponent("#payWay");
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            'listData': null
        })
        this.getOrderList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getOrderList() {
        if (this.data.pageLoad) return;
        if (this.data.pages.curPage > this.data.pages.totalPage) return;
        this.setData({
            pageLoad: true
        })
        let that = this;
        let params = {
            search_type: this.data.search_type,
            order_type: this.data.order_type,
            status: this.data.status,
            page: this.data.pages.curPage
        }
        getOrderList(params).then(response => {
            console.log(response.data)

            let data = response.data;
            
            data.lists.forEach((el,index)=>{
                if (el.finish_time && el.is_seconds==1){
                    el.total_price = (parseFloat(el.payment_amount) + parseFloat(el.seconds_price)).toFixed(2)
                }else{
                    el.total_price = (parseFloat(el.payment_amount)).toFixed(2)
                }
            })
            if (that.data.pages.curPage == 1) {
                that.setData({
                    listData: data.lists
                })
            } else {
                that.setData({
                    listData: that.data.listData.concat(data.lists)
                })
            }
            that.setData({
                'pages.curPage': that.data.pages.curPage + 1,
                'pages.totalPage': data.page.total_page,
                'pageLoad': false
            })
            if (this.data.pages.curPage >= this.data.pages.totalPage) {
                this.setData({
                    isNoMore: true
                })
            };
        }).catch(response => {
            that.setData({
                'pageLoad': false
            })
        })

    },
    changeStatus(e) {
        let dataset = e.currentTarget.dataset;
        this.setData({
            'status': dataset.status,
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            'listData': null
        })
        this.getOrderList()
    },
    changeType(e) {
        let dataset = e.currentTarget.dataset;
        this.setData({
            'status':'',
            'order_type': dataset.type,
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'isNoMore': false,
            'listData': null
        })
        this.getOrderList()
    },
    cancelOrder(e){
        let that = this;
        let dataset = e.currentTarget.dataset;
        let params={
            order_id:dataset.item.id
        }
        wx.showModal({
            title: '提示',
            content: '确定取消当前订单吗？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    cancelOrder(params).then(response => {
                        wx.hideLoading()
                        that.data.listData.splice(dataset.index,1)
                        that.setData({
                            'listData':that.data.listData
                        })

                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {
                    
                }
            }
        })
        
    },
    comfirmOrderFinish(e){
        let that = this;
        let dataset = e.currentTarget.dataset;
        let params;
        if(dataset.type == 'second'){
            params = {
                order_id: this.data.curItem.id
            }
        }else{
            params = {
                order_id: dataset.item.id
            } 
        }
        wx.showModal({
            title: '提示',
            content: '确定完成当前订单吗？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    finishOrder(params).then(response => {
                        wx.hideLoading()
                        wx.showToast({
                            icon:'none',
                            title: '订单操作完成',
                        })
                        that.data.listData.splice(dataset.index, 1)
                        that.setData({
                            'listData': that.data.listData
                        })

                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {

                }
            }
        })
    },
    cancelSecondPay(){
        this.setData({
            'isShowDetail':false
        })
    },
    showPaySeconds(e){
        let dataset = e.currentTarget.dataset;
        this.setData({
            'curItem':dataset.item,
            'curIndex':dataset.index,
            'isShowDetail': true
        })
    },
    //触发支付
    orderSecondPay() {
        let that = this;
        wx.login({
            success: res => {
                payWay._orderPayWay({
                    orderId: this.data.curItem.id,
                    code: res.code,
                    payType:'2',
                    pay_type:'wechat'
                }, function () {
                    wx.showToast({
                        icon: 'none',
                        title: '订单操作完成',
                    })
                    that.data.listData.splice(that.data.curIndex, 1)
                    that.setData({
                        'listData': that.data.listData,
                        'isShowDetail':false
                    })
                })


            }
        })
    },
    showToast(e){
        let dataset = e.currentTarget.dataset;
        wx.showToast({
            icon:'none',
            title: dataset.msg,
        })
    },
    //用户退款
    refundOrder(e){
       
        let that = this;
        let dataset = e.currentTarget.dataset;
        let params = {
            order_id: dataset.item.id
        }
        wx.navigateTo({
            url: `/pages/refund_detail_page/refund_detail_page?order_id=${dataset.item.id}&order_type=${dataset.item.order_type}`,
        })
        return;
        let msg='';
        if (dataset.type == 'apply_refund') {
            msg='确认要申请退款吗？'
        } else {
            msg = '确认要退款吗？'

        }
        wx.showModal({
            title: '提示',
            content: '确定要申请退款吗？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                    })
                    refundOrder(params).then(response => {
                        wx.hideLoading()
                        if (dataset.type == 'apply_refund' ){
                            wx.showToast({
                                icon:'none',
                                title: '提交退款申请成功，请等待店铺确认',
                            })
                        }else{
                            wx.showToast({
                                title: '退款成功',
                            })
                            
                        }
                        that.data.listData.splice(that.data.curIndex, 1)
                        that.setData({
                            'listData': that.data.listData,
                            'isShowDetail': false
                        })
                        

                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {

                }
            }
        })

    },
    askServiceHelp(e){
        let that = this;
        let dataset = e.currentTarget.dataset;
        let params = {
            order_id: dataset.item.id
        }
        let msg = '';
        wx.showModal({
            title: '提示',
            content: '确定要申请客服介入吗？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        icon:'none',
                        title: '加载中...',
                    })
                    askServiceHelp(params).then(response => {
                        wx.hideLoading()
                        wx.showToast({
                            icon:'none',
                            title: '申请客服介入成功，请等待客服服务',
                        })
                        let key = `listData[${dataset.index}].status`
                        that.setData({
                            [key]: 70
                        })


                    }).catch(response => {
                        wx.hideLoading()
                    })
                } else if (res.cancel) {

                }
            }
        })
    }
})